﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace UserInterface
{
    public class UserNetworkInterface
    {
        static StreamWriter serverWriter;
        static StreamReader serverReader;
        static TcpClient tcp;
        static string serveraddress;
        static string caraddress;
        static int port;
        static string carname;
        static int vfov;
        static int hfov;
        static double camheight;
        static double frontofcar;
        static double turningradius;

        [STAThread]
        public static void Main()
        {
            try
            {
                tcp = new TcpClient();
                int result = connectToServer();
                if (result == 1)//relay being used
                {
                    Application.Run(new UserForm(tcp));
                }
                else
                {
                    tcp.Close();
                    Application.Run(new UserForm(caraddress, 9090));
                }
            }
            catch
            {
                Console.WriteLine("Name server unavailable, using default.");
                //tcp.Close();
                try
                {
                    //Application.Run(new UserForm("kermit.aw.cl.cam.ac.uk", 9090));
                    Application.Run(new UserForm("127.0.0.1", 9090));
                    //Application.Run(new UserForm("172.31.60.160", 9090));
                }
                catch(ObjectDisposedException)
                {
                }
            }
        }

        //result 1 = direct connection
        //result 0 = relay
        static public int connectToServer()
        {
            serveraddress = "127.0.0.1";
            try
            {
                tcp.Connect(serveraddress, 9091);
                Console.WriteLine("Connection Established to Name Server.");
                serverWriter = new StreamWriter(tcp.GetStream());
                serverReader = new StreamReader(tcp.GetStream());
                string s = serverReader.ReadLine();
                Console.WriteLine("Cars available:" + s);
                for (int i = 0; i < int.Parse(s); i++)
                {
                    string carname = serverReader.ReadLine();
                    string relayordelay = serverReader.ReadLine();
                    Console.WriteLine("Choice " + (i) + " : " + carname + " - " + relayordelay);
                }
                Console.WriteLine("Please enter the number of the car you require.");
                serverWriter.WriteLine(Console.ReadLine());
                serverWriter.Flush();
                if (serverReader.ReadLine() == "DIRECT")
                {
                    string cardetails = serverReader.ReadLine();

                    caraddress = cardetails.Split(':')[0];
                    port = int.Parse(cardetails.Split(':')[1]);
                    carname = cardetails.Split(':')[2];
                    vfov = int.Parse(cardetails.Split(':')[3]);
                    hfov = int.Parse(cardetails.Split(':')[4]);
                    camheight = double.Parse(cardetails.Split(':')[5]);
                    frontofcar = double.Parse(cardetails.Split(':')[6]);
                    turningradius = double.Parse(cardetails.Split(':')[7]);
                }
                else return 1;
                return 0;
            }
            catch
            {
                return 0;
            }
        }
        public void exit()
        {
            Application.Exit();
        }
    }
}